<?php
namespace User\Controller;
use Common\Controller\MemberbaseController;
class TaskController extends MemberbaseController
{
	protected  $taskExeFun=array();
	function _initialize() {
		parent::_initialize();
	
		$this->taskExeFun=array(
						1=>"addfen",
						2=>"addRead",
						3=>"share",
						4=>""
					);
		$this->taskType=array(
				1=>"求粉丝",
				2=>"求阅读",
				3=>"求分享"
		);
		
	}
	
	public function index()
	{
		
		
	}
	public function add()
	{
		
		$this->assign("taskType",$this->taskType);
		$this->display();
		
	}
	public function make()
	{
		 $tasklist=M("taskList");
		// $url="http://demo.geekso.com/qrcode/submit";
	/*	$url="http://lab.ocrking.com/do.html";
		$img="http://kuajie.yyewt.com/tpl/simplebootx/Public/images/liantu.png";
		$result=postImgServer($url,$img);
		$as=xmlparse($result);
	*/	//if($as) echo $as;	
		//die();
		$taskType=I("get.taskType",1,"intval");
		$where["taskType"]=$taskType;
		if($taskType==1)
		{
			$where["T.status"]=1;
			$count =M("taskList")->alias("T")->join("__PUBWECHAT__ as P on T.ownuid=P.uid and T.tasktitle=P.id")->where($where)->count();
			$page = $this->page($count, 6);
			$content = M("taskList")->alias("T")->field("*, T.status as taskstatus ")->join("__PUBWECHAT__ as P on T.ownuid=P.uid and T.tasktitle=P.id")->where($where)->order("T.addtime desc")->limit($page->firstRow . ',' . $page->listRows)->select();
		}else{
			$where["status"]=1;
			$count =M("taskList")->where($where)->count();
			$page = $this->page($count, 6);
			$content=M("taskList")->field("*,status as taskstatus ")->where($where)->order("addtime desc")->limit($page->firstRow . ',' . $page->listRows)->select();
		}	
	
		//print_r($content);
		//循环获取生成二维码
		foreach($content as $key=>$val)
		{
		 	$content[$key]["erCode"]=$this->checkOrCreatCode($val["ownuid"],$val["tid"],$val["pwc_tx"]);		
		}
		$this->assign("content",$content);
		$this->assign("pager", $page->show('Admin'));
		$this->assign("taskType",$taskType);

		$this->display();

	}
	/*
	 * 检测任务二维码是否存在。存在则返回地址不存在生成，并返回地址
	 * @ $uid  用户id
	 * @ $rid  任务id
	 * @ $src  头像地址
	 * return string 
	 */
	protected  function checkOrCreatCode($uid,$rid,$src)
	{ 
		$options=get_site_options();
		$text=$options["site_host"]."/index.php/user/Auth/makeTask/uid/".$uid."/rid/".$rid;
		$fileurl="./".C("UPLOADPATH")."/taskErCode/task_".$uid."_".$rid.".png";
		if(file_exists($fileurl)){unlink($fileurl);}
			creatErCodeImg($text,$fileurl);
			//添加带头像的二维码
			if(!empty($src))
			{	
				creatErCoDeImgHeader($text,$fileurl,$src);
			}
		
		return $fileurl;
	}
	
	
	
	
	
	public function completed()
	{
		$type=I("get.type",0,"intval");
		if($type==0){return false; }
		if($type==1)
		{	
			$where["T.zuid"]=get_current_userid();
			$where["T.tasktypa"]=1;
			$where["K.taskType"]=1;
			$dbpre=C("DB_PREFIX");
			$count=M("TaskLog")
			->alias("T")
			->field("*,T.id as ltid")
			->join($dbpre."task_list AS K  on T.tid=K.tid")
			->join($dbpre."pubwechat AS P  on P.id=K.tasktitle")
			->where($where)
			->count();
			$page = $this->page($count, 10);
			$data=M("TaskLog")
			->alias("T")
			->field("*,T.id as ltid")
			->join($dbpre."task_list AS K  on T.tid=K.tid")
			->join($dbpre."pubwechat AS P  on P.id=K.tasktitle")
			->limit($page->firstRow . ',' . $page->listRows)
			->where($where)
			->select();
			$this->assign("pager", $page->show('Admin'));
			$this->assign("data",$data);
			$this->display();
		}else{
			$where["T.zuid"]=get_current_userid();
			$where["T.tasktypa"]=2;
			$dbpre=C("DB_PREFIX");
			$count=M("TaskLog")
			->alias("T")
			->field("*,T.id as ltid")
			->join($dbpre."task_list AS K  on T.tid=K.tid")
			->where($where)
			->count();
			$page = $this->page($count, 10);
			$data=M("TaskLog")
			->alias("T")
			->field("*,T.id as ltid")
			->join($dbpre."task_list AS K  on T.tid=K.tid")
			->where($where)
			->limit($page->firstRow . ',' . $page->listRows)
			->select();
			$this->assign("pager", $page->show('Admin'));
			$this->assign("data",$data);
			$this->display("completed_read");
		
		}
	}
	
	public function taskTemp()
	{
			
		
	
	}
	//分配任务
	public function assignment()
	{
		$id=I("id");
		if(!isset($this->taskType[$id])){ $this->error("任务类型不存在");}	
		$func=$this->taskExeFun[$id];
		$this->assign("taskname",$this->taskType[$id]);
		$this->$func();
	}
	public function addRead()
	{
		$_GET["typeid"]=2;
		$this->addfen();
	}
	public function share()
	{
		$id=I("get.id",0,"intval");
		$tid=I("get.tid",0,"intval");
		$this->assign("id",$id);
		$this->assign("tid",$tid);
		if($tid==1)
		{	
			$this->pubwebListUser();
		}elseif($tid==2){
			$this->firendListUser();
		}else{
			$this->delSession("pubwe");
			$this->delSession("friends");
		}
		$this->display("addtg");
		
	}
	//公众号筛选项目 
	protected  function pubwebListUser()
	{
		
		$cls=I("get.cls","","trim");
		$jf=I("get.jf","","trim");
		$fsl=I("get.fsl","","trim");
		$wechattype=I("get.wechattype",0,"intval");
		//常见分类处理
		$class=M("pubAssort")->order("listorder desc")->getField("id,t_name");
		$jifenArr=array("不限","100以下","100-500","500-1000","1000-2000","2000-5000","5000以上");
		$fensiLiang=array("不限","一万以下","1-5万","5-10万","10-20万","20-40万","40-80万","80-100万","100万以上");
		
		//分类处理数组
		$jefenact=array(
						"",
						array("LT","100"),
						array('between','100,500'),
						array('between','500,1000'),
						array('between','1000,2000'),
				   		array('between','2000,5000'),
						array('gt','5000')
		);
		$fensiact=array(
						"",
						array("LT","10000"),
						array('between','10000,50000'),
						array('between','50000,100000'),
						array('between','100000,200000'),
						array('between','200000,400000'),
						array('between','400000,800000'),
						array('between','800000,1000000'),
						array('gt','1000000')
		);
		
		//不全查询条件
		
		if(!empty($cls))
		{
			$where["commonclass"]=$cls;
		}
		if(!empty($jf))
		{
			$where["price_d"]=$jefenact[$jf];
		}
		if(!empty($fsl))
		{
			$where["pwc_fsnum"]=$fensiact[$fsl];
		}
		//公众号类型
		
		if($wechattype!=0)
		{
			$where["wechatType"]=$wechattype;	
		}
		
		$where["P.status"]=2;
		$where["U.user_type"]=2;
		$count=M("pubwechat")->alias("P")->join("__USER__ AS U on P.uid=U.id")->where($where)->count();
		$page = $this->page($count, 10);
		$pubdata=M("pubwechat")->alias("P")->field("*,P.id as wid")->join("__USER__ AS U on P.uid=U.id")->where($where)->limit($page->firstRow . ',' . $page->listRows)->select();
		$this->assign("data",$pubdata);
		$this->assign("pager", $page->show('Admin'));
		//模板输出参数
		//分类
		$this->assign("class",$class);
		$this->assign("jifenArr",$jifenArr);
		$this->assign("fensiLiang",$fensiLiang);
		
	
		$this->assign("cls",$cls);
		$this->assign("jf",$jf);
		$this->assign("fsl",$fsl);
		
		
	}
	//朋友圈选择项目
	protected  function firendListUser()
	{
		$cls=I("get.cls","","trim");
		$jf=I("get.jf","","trim");
		$fsl=I("get.fsl","","trim");
		//常见分类处理
		$class=M("circleType")->order("listorder desc")->getField("id,t_name");
		$jifenArr=array("不限","100以下","100-500","500-1000","1000-2000","2000-5000","5000以上");
		$fensiLiang=array("不限","一万以下","1-5万","5-10万","10-20万","20-40万","40-80万","80-100万","100万以上");
		
		//分类处理数组
		$jefenact=array(
				"",
				array("LT","100"),
				array('between','100,500'),
				array('between','500,1000'),
				array('between','1000,2000'),
				array('between','2000,5000'),
				array('gt','5000')
		);
		$fensiact=array(
				"",
				array("LT","10000"),
				array('between','10000,50000'),
				array('between','50000,100000'),
				array('between','100000,200000'),
				array('between','200000,400000'),
				array('between','400000,800000'),
				array('between','800000,1000000'),
				array('gt','1000000')
		);
		
		//不全查询条件
		
		if(!empty($cls))
		{
			$where["circle"]=$cls;
		}
		if(!empty($jf))
		{
			$where["price"]=$jefenact[$jf];
		}
		if(!empty($fsl))
		{
			$where["friendnum"]=$fensiact[$fsl];
		}
		
		$where["status"]=2;
		$count=M("WechatNumber")->where($where)->count();
		$page = $this->page($count, 10);
		$pubdata=M("WechatNumber")->where($where)->limit($page->firstRow . ',' . $page->listRows)->select();
		$this->assign("data",$pubdata);
		$this->assign("pager", $page->show('Admin'));
		//模板输出参数
		//分类
		$this->assign("class",$class);
		$this->assign("jifenArr",$jifenArr);
		$this->assign("fensiLiang",$fensiLiang);
		
		
		$this->assign("cls",$cls);
		$this->assign("jf",$jf);
		$this->assign("fsl",$fsl);
		
		
	}
	//临时存储选中的公众号值
	public function getSelectPub()
	{
		$temp=I("post.temp");
		$chileuser=I("post.chileuser");
		if(empty($chileuser)){$chileuser=array();}
		if($_SESSION[$temp])
		{
			$tempval=$_SESSION[$temp];
			$_SESSION[$temp]=array_unique(array_merge($tempval,$chileuser));
		}else{
			$_SESSION[$temp]=$chileuser;
		}	
		if(empty($_SESSION[$temp])){$this->error("请至少选择一项");}
		$data=$this->getAppoint($_SESSION[$temp],$temp);
		$this->success($data);	
	}
	private function delSession($key)
	{
		if(!empty($_SESSION[$key])){unset($_SESSION[$key]);}
	}
	//删除选中的某个公众号
	public function delSelectPub()
	{
		$id=I("post.id");
		$temp=I("post.temp");
		if(!empty($_SESSION[$temp]))
		{
			$tempval=$_SESSION[$temp];
			$key=array_search($id, $tempval);
			if($key) unset($tempval[$key]);
			$_SESSION[$temp]=$tempval;
		}	
		//$this->success("成功");
		$data=$this->getAppoint($_SESSION[$temp],$temp);
		$this->success($data);
	}
	
	
	//$type pubwe  friend
	private function getAppoint($arrid=array(),$type="pubwe")
	{
		if($type=="pubwe")
		{
			$D=M("pubwechat");
			$field="id";
		}else{
			$D=M("wechatNumber");
			$field="wid";
		}
		$where[$field]=array("in",$arrid);
		$DATA=$D->where($where)->select();
	
		return $DATA;
	}
	//添加求推广任务
	
	public function addshare()
	{
		$types=I("get.types");
		
		if($types=="pubwec")
		{
			$this->addpubwectask();
		}elseif($types=="friends"){
			$this->addfriendstask();
		}else{	
			$this->addBroadCast();
		}	
	}
	//添加广投任务
	
	protected  function  addBroadCast()
	{
	    
	    $task=M("taskHall");
	    $rules = array(
	        //array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
	        array('wid', 'require', '请填写微信号', 1 ),
	        array('ercode', 'require', '请填写微信二维码', 1 ),
	        array('title', 'require', '请填写任务标题', 1 ),
	        array('title', 'require', '请填写任务标题', 1 ),
	        array('askfor', 'require', '推广要求', 1 ),
	        array('standard', 'require', '审核标准', 1 ),
	        array('integral', 'require', '奖励积分', 1 )
	    );
	    if($task->validate($rules)->create()===false)
	    {
	        $this->error($task->getError());
	    }else{
	        $task->posttime=time();
	        $task->uid=get_current_userid();
	       $result=$task->add();
	        if($result){$this->success("添加成功，请耐心等待审核");}
	        else{ $this->error("未添加成功"); }
	    }    
	   
	}
	
	//添加公众号任务
	public function addpubwectask()
	{
		if(empty($_SESSION["pubwe"])) $this->error("请选择公众号 ");
		$pubwe=$this->getAppoint($_SESSION["pubwe"],"pubwe");
		$task=M("taskShareList");
		$starttime=I("post.starttime");
		$rules = array(
				//array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
				array('title', 'require', '请填写标题！', 1 ),
				array('cover', 'require', '请上传封面图片！', 1 ),
				array('content', 'require', '请填写推广的正文！', 1 ),
				array('starttime', 'require', '请填写开始时间！', 1 ),
				array('loction','require','请选择广告位置',1)
	
		);
		if($task->validate($rules)->create()===false)
		{
			$this->error($task->getError());
		}else{
			//计算一下粉丝总量
			$fsnum=0;
			//计算所需积分
			$field=array("1"=>"price_d",
					"2","price_f",
					"3","price_s",
					"4","price_o",
			);
			$loction=I("get.loction",1,"intval");
			$countMoney=0;
			foreach($pubwe as $key=>$val)
			{
				$fsnum+=$val["pwc_fsnum"];
				$countMoney+=$val[$field[$loction]];
			}
			$task->countmoney=$countMoney;
			$task->fsnum=$fsnum;
		    $task->posttime=time();
		    $task->types=1;
		    $task->uid=get_current_userid();
		    $task->user=serialize($pubwe);
		    $task->endtime=strtotime("+24 hours",strtotime($starttime)); 
		    $result=$task->add();
		    if($result)
		    {
		    	$this->delSession("pubwe");
		    	$this->success("发布成功",U("user/task/settleaccounts",array("tid"=>$result)));
		    }else{
		    	$this->error("提交发生意外，请稍后再试");
		    }
		}
	}
	
	//添加朋友圈任务
	public function addfriendstask()
	{
		if(empty($_SESSION["friends"])) $this->error("请选择公众号 ");
		$friends=$this->getAppoint($_SESSION["friends"],"friends");
		$task=M("taskShareList");
		$starttime=I("post.starttime");
		$taskact=I("post.taskact",1,"intval");
		
		$cmsg=$taskact==1?"请填写分享内容":"请填写直发内容";
		$rules = array(
				//array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
				array('title', 'require', '请填写标题！', 1 ),
				array('content', 'require', $cmsg, 1 ),
				array('starttime', 'require', '请填写开始时间！', 1 )
		);
		if($task->validate($rules)->create()===false)
		{
			$this->error($task->getError());
		}else{
			//计算一下好友总量
			$fsnum=0;
			//计算所需积分

			$countMoney=0;
			foreach($friends as $key=>$val)
			{
				$fsnum+=$val["friendnum"];
				$countMoney+=$val["price"];
			}
			$task->countmoney=$countMoney;
			$task->fsnum=$fsnum;
			$task->posttime=time();
			$task->types=2;
			$task->uid=get_current_userid();
			$task->user=serialize($friends);
			$task->endtime=strtotime("+24 hours",strtotime($starttime));
			//附加信息
			if($taskact==1)
			{
				$this->fjnr=I("post.fjnr");
			}else{
				$this->fjnr=I("post.peitu");
			}
			$result=$task->add();
			if($result)
			{
				$this->delSession("friends");
				$this->success("发布成功",U("user/task/settleaccounts",array("tid"=>$result)));
			}else{
				$this->error("提交发生意外，请稍后再试");
			}
		}
	}
	
	
	public function settleaccounts()
	{
		$tid=I("get.tid",0,"intval");
		//首先判断任务状态
		$task=M("taskShareList");
		$result=$task->where("tid=$tid")->find();
		if($result["status"]==0)
		{
			//$hasmoney=$this->jisuanTaskInt($tid);
			$hasmoney=$result["countmoney"];
			$userid=sp_get_current_userid();
			$uwhere["uid"]=$userid;
			$Usermoney=M("Money")->where($uwhere)->find();
			if($hasmoney>=$Usermoney["available_funds"])
			{
				$this->assign("status",0); //资金不足
			
			}else{
				//分派任务
				$this->apportiontask($tid);
				$mdata["available_funds"]=$Usermoney["available_funds"]-$hasmoney;
				$mdata["freeze_funds"]=$Usermoney["freeze_funds"]+$hasmoney;
				$KJ=M("Money")->where($uwhere)->save($mdata);
				if($KJ)
				{
					moneyLog($userid,$hasmoney,"冻结资金",2);
				}
				$this->assign("status",1); //冻结资金

			}
			$this->assign("available_funds",$Usermoney["available_funds"]);
			$this->assign("money",ceil($hasmoney/C("sys_Ad_IntegralProportion")));
			$this->assign("hasmoney",$hasmoney);
			
		}else{
				
			$this->assign("status",2);
			
		}
		
		$this->display(":hint");
		
	}
	
	//检测账户资金是否充足
	protected function checkeMoneyIsGou($money)
	{
		$userid=sp_get_current_userid();
		$Usermoney=M("Money")->where(array("uid"=>$userid))->find();
		if($money>=$Usermoney["available_funds"])
		{
			return false;
		}
		
	}
	//计算任务需要的扣除的积分
	protected function jisuanTaskInt($tid)
	{
		$task=M("taskShareList");
		$result=$task->where("tid=$tid")->find();
	
		//任务参与用户
		$user=unserialize($result["user"]);
		//广告位置 
		$loction=$result["loction"];
		//计算所需积分
		$field=array("1"=>"price_d",
					"2","price_f",
					 "3","price_s",
					"4","price_o",
					);
		$countMoney=0;
		foreach($user as $key=>$val)
		{
			$countMoney+=$val[$field[$loction]];	
		}
		return $countMoney;
	}
	//分派任务
	protected  function  apportiontask($tid)
	{
		$task=M("taskShareList");
		$userid=sp_get_current_userid();
		$result=$task->where("tid=$tid")->find();
		if($result["status"]==0)
		{
			$user=unserialize($result["user"]);
			$Utask=M("taskShareListuser");
			foreach($user as $key=>$val)
			{
				$data["tid"]=$tid;
				$data["uid"]=$val["uid"];
				$data["gid"]=$userid;
				$data["types"]=$result["types"];
				$Utask->add($data);
			}
			$tdata["status"]=1;
			$task->where("tid=$tid")->save($tdata);
		}else{
			return false;
		}
		
	}
	
	
	
	
	
	
	
	
	
	public function addfen()
	{
		//print_r(C("sys_taskcess"));
		if(IS_POST)
		{
			$tasklist=M("taskList");
			$typeid=I("post.typeid",1,"intval");
			$tishi=$typeid==1?"请选择公众号":"请填写公众号发布的文章访问地址";
			if($typeid==2)
			{
				$title=I("post.title","","trim");
				if(empty($title)){$this->error("文章标题必须填写");}
			}	
			$tasknum=I("post.tasknum",0,"intval");
			$rules = array(
					//array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
					array('reward', 'require', '任务奖励积分必须填写！', 1 ),
					array('reward', 'number', '任务奖励积分填写不正确！', 1 ),
					array('tasktitle','require',$tishi,1)
			);
			if($tasklist->validate($rules)->create()===false)
			{
				$this->error($tasklist->getError());
			}else{
				$tasklist->tasknum=$tasknum;
				$tasklist->taskType=$typeid;
				$beilu=$this->countTaskInt();
				$reward=I("post.reward");
				$tasklist->integration=ceil($reward*$beilu);
				$tasklist->addtime=time();
				$tasklist->ownuid=get_current_userid();
				$result=$tasklist->add();
				if($result)
				{	
					$this->success("提交成功",$this->taskMangList[$typeid]);
				}else{
					$this->error("提交失败");
				}
			}
				
		}
		$typeid=I("get.typeid",1,"intval");
		if($typeid==1)
		{
			$pubdata=$this->getPubWec();
			$this->assign("pubdata",$pubdata);
		}
		$this->assign("typeid",$typeid);
		$this->assign("beilu",$this->countTaskInt());
		$this->assign("url",U("user/task/addfen"));			
		$this->display("addfen");
	}
	
	
	//任务执行扣除积分倍率
	public function countTaskInt()
	{
		$taskcess=intval(C("sys_taskcess")); //获取任务执行税利
		if($taskcess>100){$taskcess=100;}
		return $taskcess*0.01+1;
	}
	
	//获取用户所拥有的公众号列表
	public function getPubWec()
	{
		$uid=get_current_userid();
		$pubdata=M("pubwechat")->where("uid= $uid and status=2 ")->select();
		if(!$pubdata)
		{
			$this->display("addfront");die();	
		}
		return $pubdata;
	}
	
	//管理求粉丝任务
	public function fenMang()
	{
		$this->QiuFen(1);
	}
	public function readMang()
	{
		$this->QiuFen(2);	
	}
	public function QiuFen($type)
	{
		//echo MODULE_NAME;
		//echo CONTROLLER_NAME;
		//echo ACTION_NAME;die();
		$where["ownuid"]=get_current_userid();
		$where["taskType"]=$type;
		$status=I("get.status",0,"intval");
		if($status==1)
		{
			$In=array("1");
		}elseif($status==2){
			$In=array("2");
		}else{
			$In=array("1","2");
		}
		if($type==1)
		{	
			$where["T.status"]=array("in",$In);
			$count =M("taskList")->alias("T")->join("__PUBWECHAT__ as P on T.ownuid=P.uid and T.tasktitle=P.id")->where($where)->count();
			$page = $this->page($count, 10);
			$content = M("taskList")->alias("T")->field("*, T.status as taskstatus ")->join("__PUBWECHAT__ as P on T.ownuid=P.uid and T.tasktitle=P.id")->where($where)->order("T.addtime desc")->limit($page->firstRow . ',' . $page->listRows)->select();
		}else{
			$where["status"]=array("in",$In);
			$count =M("taskList")->where($where)->count();
			$page = $this->page($count, 10);
			$content=M("taskList")->field("*,status as taskstatus ")->where($where)->order("addtime desc")->limit($page->firstRow . ',' . $page->listRows)->select();
		}
		$this->assign("content",$content);
		$this->assign("pager", $page->show('Admin'));
		$this->assign("status",$status);
		$this->assign("func",ACTION_NAME);
		$this->assign("taskType",$type);
		$this->display("fenMang");
	}
	//删除任务
	public function delMang()
	{
		$tid=I("get.tid");
		$where["tid"]=$tid;
		$where["ownid"]=get_current_userid();
		$data["status"]=3;
		$result=M("taskList")->where($where)->save($data);
		if($result)
		{
			$this->success("删除成功");
		}else{
			$this->error("删除失败");	
		}	
	}
	//修改任务
	public function changeTask()
	{
		$uid=get_current_userid();
		$where['tid']=I("get.tid",0,"intval");
		$where['ownid']=$uid;
	
		$info=M("taskList")->where($where)->find();
		if(!info){$this->error("不存在此任务");}
		if($info["status"]==1)
		{
			$data["status"]=2;
		}else{
			$data["status"]=1;
		}
		$result=M("taskList")->where($where)->save($data);
		if($result)
		{
			$this->success("修改成功");
		}else{
			$this->error("修改失败");
		}
		//获取公众号列表
	/*	if($info["taskType"]==1)
		{	
			$pwhere["uid"]=$uid;
			$pwhere["id"]=$info["tasktitle"];
			$pub=M("pubwechat")->where($pwhere)->find();
			$this->assign("pub",$pub);
		}
		$this->display("Changefen");
	*/
	}
	
	
	//分享任务管理
	public function ForwardMang()
	{
		$type=I("get.types",0,"intval");
		$task=M("taskShareList");
		$where["uid"]=get_current_userid();
		if($type==1)
		{	
			$where["types"]=1;
			$this->assign("loc",array("1"=>"单图文","2"=>"第一条图文","3"=>"第二条图文","4"=>"第三-N条","5"=>"朋友圈"));
			$this->assign("types",1);
		}else{
			$where["types"]=2;
			$this->assign("types",2);
		}
		$this->assign("status",array("待公布","已派单","已完成","已取消"));
		$data=$task->where($where)->order("posttime desc")->select();
		$this->assign("data",$data);
		$this->display("forwardmang");	
		//$this->display(":hint");
	}
	//服务号资源
	public function resourceService()
	{
		
		$_GET["wechattype"]=1; //服务号
		$this->pubwebListUser();
		$this->assign("tid",1);
		$this->assign("tasktitle","服务号资源");
		$this->assign("func",ACTION_NAME);
		$this->display("resource");	
		
	}
	//订阅号资源
	public function resourceSubscribe()
	{
		$_GET["wechattype"]=2; //服务号
		$this->pubwebListUser();
		$this->assign("tid",1);
		$this->assign("tasktitle","订阅号资源");
		$this->assign("func",ACTION_NAME);
		$this->display("resource");
		
	}
	//朋友圈资源
	public function resourceFriend()
	{
		$this->firendListUser();
		$this->assign("tid",2);
		$this->assign("tasktitle","朋友圈资源");
		$this->assign("func",ACTION_NAME);
		$this->display("resource");
	}
	//我接到的分享任务
	public function poputask()
	{
		$type=I("get.type",1,"intval");
		$uid=get_current_userid();
	
			$where["LU.uid"]=$uid;
			$where["LU.types"]=$type;
			$result=M("taskShareListuser")
			->alias("LU")
			->join("__TASK_SHARE_LIST__ AS L ON LU.tid=L.tid")
			->where($where)
			->select();
			
		foreach($result as $key=>$val)
		{
			$usersPub=unserialize($val["user"]);
			unset($result[$key]["user"]);
			foreach($usersPub as $k=>$v)
			{		
				if($v["uid"]==$uid)
				{
					$result[$key]["userpub"]=$v;
					break;
				}
			}
		}	
		
		$this->assign("types",$type);
		$this->assign("data",$result);
		$this->display("popupwb");
	}
	
//做任务用户查看任务详情
 public function taskInfo()
 {
 	$this->_taskInfo();
 	$this->display("taskinfo");
 }
 public function makeover()
 {
 	if(IS_POST)
 	{
 		
 		
 	}
 	$this->_taskInfo();
 	$this->display("makeover");
 	
 }
 
	
 protected  function _taskInfo($tid)
 {
 	$tid=empty($tid)?I("get.tid",0,"intval"):$tid;
 	$task=M("taskShareList");
 	$uid=get_current_userid();
 	$result=$task->where("tid=$tid")->find();
 	$usersPub=unserialize($result["user"]);
 	unset($result["user"]);
 	foreach($usersPub as $k=>$v)
 	{
		$result["userpub"][$k]=$v;
 	}
 	$this->assign($result);
 }
 public function agree()
 {
 	$id=I("get.id",0,"intval");
 	$this->changeUserTaskState($id, 1);
 	$this->success("接单成功");
 
 }
 public function reject()
 {
 	$id=I("get.id",0,"intval");
 	$this->changeUserTaskState($id, 3);
 	$this->success("拒单成功");
 }
 protected  function changeUserTaskState($id,$state)
 {
 	$tk=M("taskShareListuser");
 	$uid=get_current_userid();
 	$where["id"]=$id;
 	$data["state"]=$state;
 	$tk->where($where)->save($data);
 }

 function test()
 {
 	print_r(C());	
 
 }
 
 
 
	
}